package com.hustzp.com.xichuangzhu.me;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.avos.avoscloud.SaveCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;
import com.hustzp.com.xichuangzhu.utils.PhoneNumberTools;

/**
 * Created by Administrator on 2016/6/8.
 */
public class ChangePhoneNoActivity extends MyBaseActivity {

    private EditText phoneNum;
    private Button sendBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone_no);

        setToolbar();
        initViews();
    }

    private void setToolbar() {
        TextView navTxt = (TextView) findViewById(R.id.tv_nav_txt);
        navTxt.setText(getString(R.string.account_and_passwd));
        TextView appTitle = (TextView) findViewById(R.id.tv_title_txt);
        appTitle.setText(getString(R.string.acount_change_phonenumber));
    }

    private void initViews() {
        phoneNum = (EditText) findViewById(R.id.tv_phone_number);
        sendBtn = (Button) findViewById(R.id.send_button);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(phoneNum.getText().toString().trim())) {
                    DialogFactory.hintDialog(ChangePhoneNoActivity.this, getString(R.string.phone_number_not_empity));
                    return;
                }

                if (!PhoneNumberTools.verifyPhoneNumber(phoneNum.getText().toString().trim())) {
                    DialogFactory.hintDialog(ChangePhoneNoActivity.this, "请输入正确的手机号码");
                    return;
                }

                final Dialog loading = DialogFactory.createLoadingDialog(ChangePhoneNoActivity.this, "");
                AVOSCloud.requestSMSCodeInBackground(phoneNum.getText().toString().trim(), getResources().getString(R.string.app_name), "更改手机号码",
                        10, new RequestMobileCodeCallback() {
                            @Override
                            public void done(AVException e) {
                                loading.dismiss();
                                if (e == null) {
                                    AVUser.getCurrentUser().setMobilePhoneNumber(phoneNum.getText().toString().trim());
                                    AVUser.getCurrentUser().saveInBackground(new SaveCallback() {
                                        @Override
                                        public void done(AVException e) {
                                            if (e == null) {
                                                // 退出重新登录
                                                setResult(RESULT_OK);
                                                finish();
                                            }
                                        }
                                    });

                                } else {
                                    DialogFactory.hintDialog(ChangePhoneNoActivity.this, getResources().getString(R.string.sms_send_error));

                                }
                            }
                        });

            }
        });
    }
}
